package com.tsystems.clientEmployee.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.tsysstems.clientEmployee.ClientEmployee;
import com.tsystems.entities.Employee;

/**
 * @author vlad
 * 
 */
public class LoginPanel extends JPanel {
	static JDialog dialog;

	private static final long serialVersionUID = -7733781114127633674L;

	private JPasswordField password;
	private JTextField login;
	private String result;

	public LoginPanel() {
		super(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 5, 10));

		add(new JLabel("Имя пользователя"), new GridBagConstraints(0, 0, 1, 1,
				10.0, 20.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));

		login = new JTextField();
		login.setColumns(15);
		add(login, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Пароль"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 10, 10));

		password = new JPasswordField();
		add(password, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		JButton registerButton = new JButton("Регистрация");
		JButton okBtn = new JButton("Войти");
		JButton cancelBtn = new JButton("Выход");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(registerButton);
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);

		add(buttonPanel, new GridBagConstraints(0, 2, 2, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Employee employee = RegisterPanel.showDialog(JOptionPane
						.getRootFrame());

				if (employee == null) {
					return;
				}

				String result = ClientEmployee.getInstance().register(employee);
				if (result != null) {
					JOptionPane.showMessageDialog(LoginPanel.this, result);
				} else {
					JOptionPane
							.showMessageDialog(LoginPanel.this,
									"Внутренняя проблема приложения, обратитесь к администратору!");
				}
			}
		});

		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientEmployee clientEmployee = ClientEmployee.getInstance();
				String response = clientEmployee.login(login.getText(),
						new String(password.getPassword()));
				if (response == null) {
					JOptionPane
							.showMessageDialog(LoginPanel.this,
									"Внутренняя проблема приложения, обратитесь к администратору!");
				}
				if (response.equals("Вход в систему успешно выполнен")) {
					result = login.getText();
					LoginPanel.this.getTopLevelAncestor().setVisible(false);
				} else {
					JOptionPane.showMessageDialog(LoginPanel.this, response);
				}
			}
		});

		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result = null;
				ClientEmployee.getInstance().close();
				dialog.dispose();
			}
		});
	}

	private String getResult() {
		return result;
	}

	public static String showDialog() {
		dialog = new JDialog((JDialog) null, "Вход", true);
		LoginPanel panel = new LoginPanel();

		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return panel.getResult();
	}

}
